package com.hu.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hu.common.BaseResponse;
import com.hu.common.ErrorCode;
import com.hu.common.PageRequest;
import com.hu.common.ResultUtils;
import com.hu.common.question.QuestionDto;
import com.hu.constant.UserConstant;
import com.hu.exception.ThrowUtils;
import com.hu.pojo.dto.question.QuestionAddRequest;
import com.hu.pojo.dto.question.QuestionBatchDeleteRequest;
import com.hu.pojo.dto.question.QuestionQueryRequest;
import com.hu.pojo.dto.question.QuestionUpdateRequest;
import com.hu.pojo.entity.Question;
import com.hu.pojo.vo.QuestionVO;
import com.hu.service.QuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;

/**
 * 题目接口
 */
@RestController
@RequestMapping("/question")
@Slf4j
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 创建题目 TODO 指定题库,并且存入关联表,给普通用户提供一个公共题库
     * TODO 只要给公共题库发了一条信息,那么就给管理员推送信息--管理员可以进行审核,归纳
     * @param questionAddRequest
     * @return
     */
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    @SaCheckPermission(value = {UserConstant.ADD_PERMISSION,UserConstant.ALL_PERMISSION},mode = SaMode.OR)
    public BaseResponse<Boolean> addQuestion(@Validated @RequestBody QuestionAddRequest questionAddRequest) {
        return ResultUtils.success(questionService.saveQuestion(questionAddRequest));
    }


    /**
     * 更新题目
     * 管理员&有更新权限的可以更新/是所属人也可更改
     * @param questionUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest) {
        boolean result = questionService.updateQuestion(questionUpdateRequest);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取题目
     * TODO 加入频控
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionVO> getQuestionVOById(@Validated @Min(value = 0) long id) {
        Question question = questionService.getQuestionById(id);
        ThrowUtils.throwIf(question == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(QuestionVO.objToVo(question));
    }

    /**
     * 分页获取题目列表 TODO 管理员可以搜出没有批准的题目
     *
     * @param questionQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest) {
        ThrowUtils.throwIf(questionQueryRequest == null, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<Question> questionPage = questionService.listQuestionByPage(questionQueryRequest);
        return ResultUtils.success(questionPage);
    }

    /**
     * 提供的接口 ==> user模块
     * 分页获取当前登录用户创建的题目列表
     * @return
     */
    @PostMapping("/list/my/question")
    public BaseResponse<Page<QuestionDto>> listMyQuestionPage(@RequestBody PageRequest pageRequest) {
        Page<QuestionDto> pages = questionService.getMyQuestionPage(pageRequest);
        return ResultUtils.success(pages);
    }


    /**
     * 批量删除 (兼并了单个删除)
     * 管理员&&拥有相关权限(管理页面) || 本人创建的题目(查看我的题目进行修改)
     * @param questionBatchDeleteRequest
     * @return
     */
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteQuestions(@Validated @RequestBody QuestionBatchDeleteRequest questionBatchDeleteRequest) {
        questionService.batchDeleteQuestions(questionBatchDeleteRequest.getQuestionIdList());
        return ResultUtils.success(Boolean.TRUE);
    }


    /**
     * es 操作
     * @param questionQueryRequest
     * @param request
     * @return
     */
/*    @PostMapping("/search/page/vo")
    public BaseResponse<Page<QuestionVO>> searchQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
                                                                 HttpServletRequest request) {
        long size = questionQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 200, ErrorCode.PARAMS_ERROR);
        Page<Question> questionPage = questionService.searchFromEs(questionQueryRequest);
        return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
    }*/

}


